<?php

namespace App\Http\Controllers\Admin\Tags;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\Tags\Api\TagsApi;
use App\Model\Tags;

class TagsResourceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $tagsList = TagsApi::getTagsList();

        return view('Admin/Tags/list', [ 'tagsList' => $tagsList]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   

        return view('Admin/Tags/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   

        $tagName = $request->input('tagname');

        $result = TagsApi::tagsStore($tagName);

        return $result;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $tagInfo = TagsApi::getTagInfo($id);

        return view('Admin/Tags/edit', [ 'tagInfo' => $tagInfo ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   
        //输入信息的验证
        $this->validate($request, [

               'name' => 'bail|required|max:20' 

            ], [

                'name.required' => '标签名必需填写',
                'name.max' => '标签名不能超过20个字符',
            ]);

        $result = TagsApi::tagsUpdate($request, $id);

        if ($result['code'] == 200) {

            return Redirect('admin/tag')->with([

                    'tipscode' => $result['code'],
                    'tips' => $result['msg'],
                ]);
        } else if ($result['code'] == 400) {

            return back()->with([

                    'tipscode' => $result['code'],
                    'tips' => $result['msg'],
                ]);
        }


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = TagsApi::tagsDel($id);

        return $result;
    }
}
